<?php
$exp_pic = array("jpg","JPG","png","PNG","gif","GIF","jpeg","JPEG",""); // разширения за картинката
$max_size_pic = 2048; // запишете максималния размер на картинката(в KB)
$pic_path = "pictures/"; // къде да се записват файловете
$max_width = 600; // на колко да се промени ширината на картинката, ако е по голяма

if(isset($_SESSION["user"]))
{
	//$user = $_SESSION["user"];
	if(isset($_POST["publishContent"]))
	{
		if ($_POST["pic_source"] == "from_url")
		{
			$_POST['pic'] = "";
				
			$pic_tmp = $_POST ['pic_url'];
			if ($pic_tmp == "Линк към картинката")
			{
				$pic_tmp = "";
			}
			$pic_name = explode("/", $pic_tmp);
			$pic_name = end($pic_name);
			$pic_name = strtolower($pic_name);
			$pic_size = Content::getRemoteFileSize($pic_tmp);
		}
		elseif ($_POST['pic_source'] == "from_comp")
		{
			$_POST['pic_url'] = "";
			$pic_name = $_FILES['pic']['name'];
			$pic_tmp = $_FILES['pic']['tmp_name'];
			$pic_size = $_FILES['pic']['size'];
		}
		
		

		$pic_extension = explode(".", $pic_name);
		$pic_extension = end($pic_extension);
		$pic_extension = strtolower($pic_extension);
		
		if (in_array($pic_extension, $exp_pic))
		{


			if ($pic_size <= ($max_size_pic * 1024))
			{

					$pic_path = preg_replace("/([\/])+/", "\\1", $pic_path."/");
					$ex_pic = explode(".", $pic_name);
					$ex_pic = end($ex_pic);
					$new_name_pic = Content::new_name($ex_pic);

					while (file_exists($pic_path.$new_name_pic)) { $new_name_pic = new_name($ex_pic); }
					
					$gis = GetImageSize($pic_tmp);
					$type = $gis[2];
					if($type == "1")
					{
						if(is_uploaded_file($pic_tmp))
						{
							if(move_uploaded_file($pic_tmp,$pic_path.$new_name_pic))
							{
								$error = "Gotovo";
								echo $pic_tmp;
							}
							else 
								$error = "Error2";
						}
						else 
						{
							echo $pic_tmp;
							$error = "Error1";
						}
					}
					else
					{
						if (Content::img_resize( $pic_tmp , $max_width , $pic_path , $new_name_pic))
						{
							$pic_info = $pic_path.$new_name_pic;
							DBOperations::insetrtContentIntoDB("pic",$pic_info,$user->getUser_id(),$_POST["title"]);
							$error = "Gotovo";
						}
					}
			}
			else
			{
				$error = "Картинката не трябва да превишава $max_size_pic KB!";
			}


		}
		else
		{
			$error = "Позволени са само следните разширения за картинка: ".implode(",", $exp_pic);
		}
	}
	
	if(isset($error))
	{
		echo $error;
	}

	?>

	<form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER["REQUEST_URI"]; ?>" >
		<p>Вид на публикацията: </p>
		<select name="type" id="type" onChange="
		showElementOption(this.value,'joke','optionsForJoke');
		showElementOption(this.value,'pic','optionsForPic');
		showElementOption(this.value,'pic','titlePlace');
		showElementOption(this.value,'video','');
		showElementOption(this.value,'video','titlePlace');">
			<option value="joke">Виц</option>
			<option value="pic">Картинка</option>
			<option value="video">Видео</option>
		</select>
			
			<br />
			<div id="optionsForPic" style="display: none;">
			<input type="radio" name="pic_source" value="from_url" id="image_from_url" onfocus="showElement('from_url');hideElement('from_comp');return true;" onchange="showElement('from_url');hideElement('from_comp');return true;" /><label for="image_from_url">От URL</label>
			
			<input type="radio" name="pic_source" value="from_comp" id="image_from_comp" onfocus="showElement('from_comp');hideElement('from_url');return true;" onchange="showElement('from_comp');hideElement('from_url');return true;" /><label for="image_from_comp">От този компютър</label>
			
			<br />
			
			<div id="from_url" style="display: none;">
				<input style="border:1px solid #cccccc;font-size:13px;height:16px;line-height:16px;padding:2px 4px;width:400px;" type="text"  name="pic_url" value="Линк към картинката" OnFocus="if(this.value=='Линк към картинката') this.value='';" OnBlur="if(this.value=='') this.value='Линк към картинката';" />
			</div>
			<br />
			<div id="from_comp" style="display: none;">
				<input onmouseover="hover();" onmouseout="hoverout();" type="file" name="pic" size="44"/>
			</div>
			</div>
			<div id="titlePlace" style="display: none;" >
				<p>Заглавие:</p>
				<input type="text" name="title" />
			</div>
			<div id="optionsForJoke" style="display: inline;">
				<p>Виц:</p>
				<textarea name="content"></textarea>
			</div>
			<br />
			<input type="submit" name="publishContent" value="Публикувай" />
	</form>
	<?php
}
else
{
	echo "Трябва да сте влезли в акаунта си, за да можете да публикувате!";
}
?>